1 Initialization

1.1 Read Logs and Create Data Container Objects


In [1]:
%matplotlib inline
import LogReader as lr
import LogDataStructures as ls
fieldopt_path = "/home/einar/Documents/GitHub/PCG/FieldOpt/"
output_dir_path = "/home/einar/fo_out_20161118/"
hdf5_summary_file_name = "5SPOT.vars.h5"
reader = lr.LogReader(output_dir_path)
case_container = ls.CaseContainer(reader.case_log, reader.property_uuid_name_map)
optimizer = ls.Optimizer(reader.optimization_log, case_container)
simulator = ls.Simulator(reader.simulation_log)
production_data = ls.ProductionData(reader.production_data_log)

1.2 Extract Interesting Cases


In [2]:
case_base = case_container.evaluated_cases[0]
case_tentative_best = optimizer.best_case_pr_iteration[-1]
case_latest = case_container.evaluated_cases[-1]

1.3 Extract Interesting Wells


In [3]:
well_base_injector = case_base.get_well('INJECTOR')
well_base_producer = case_base.get_well('PRODUCER')
well_tentative_best_injector = case_tentative_best.get_well('INJECTOR')
well_tentative_best_producer = case_tentative_best.get_well('PRODUCER')
well_latest_injector = case_latest.get_well('INJECTOR')
well_latest_producer = case_latest.get_well('PRODUCER')

2 Optimizer Data

2.1 Current Status


In [4]:
print('Current iteration:\t', optimizer.current_iteration)
print('Current step length:\t', optimizer.current_step_length)
print('Number of evaluated cases simulated in total:\t\t', optimizer.number_of_evaluated_cases)
print('Number of cases simulated this iteration:\t\t', optimizer.number_of_evaluated_cases_in_iteration)
print('Number of cases yet to be simulated this iteration:\t', optimizer.number_of_queued_cases_in_iteration)


Current iteration:	 24
Current step length:	 32
Number of evaluated cases simulated in total:		 554
Number of cases simulated this iteration:		 1
Number of cases yet to be simulated this iteration:	 23

2.2 Objective Function Values


In [5]:
from matplotlib import pyplot as plt
fig_ofv = plt.figure()
fig_ofv.set_size_inches(10,6)
ax_ofv = fig_ofv.add_subplot(111)
ax_ofv.plot([i+1 for i in range(len(optimizer.best_case_pr_iteration))],
        [c.objective_function_value for c in optimizer.best_case_pr_iteration])
ax_ofv.set_xlabel('Iteration')
ax_ofv.set_ylabel('Objective function value')
ax_ofv.set_title('Objective function values per iteration')


Out[5]:
<matplotlib.text.Text at 0x7f0a41460908>

In [6]:
print('Iteration\tObjective function value')
for i in range(len(optimizer.best_case_pr_iteration)):
    print (i+1, '\t\t', optimizer.best_case_pr_iteration[i].objective_function_value)


Iteration	Objective function value
1 		 8750.3
2 		 33273.3
3 		 99444.0
4 		 113405.0
5 		 126038.0
6 		 137600.0
7 		 145337.0
8 		 159748.0
9 		 212414.0
10 		 242423.0
11 		 278652.0
12 		 307053.0
13 		 315263.0
14 		 329851.0
15 		 334231.0
16 		 337602.0
17 		 370506.0
18 		 376964.0
19 		 376964.0
20 		 388122.0
21 		 395737.0
22 		 399260.0
23 		 399260.0
24 		 399260.0

3 Well Paths

3.1 Common Definitions


In [7]:
import matplotlib.gridspec as gridspec
grid_spec_wp = gridspec.GridSpec(12,12)

def setup_wp_figure(fig):
    fig.set_size_inches(13,12)
    ax_3d = fig.add_subplot(grid_spec_wp[0:8,:], projection='3d')
    ax_xy = fig.add_subplot(grid_spec_wp[8:12,0:4])
    ax_xz = fig.add_subplot(grid_spec_wp[8:12,4:8])
    ax_yz = fig.add_subplot(grid_spec_wp[8:12,8:12])
    fig.tight_layout(pad=2.5, w_pad=2.5, h_pad=2.5)
    
    ax_3d.set_xlabel('x')
    ax_3d.set_ylabel('y')
    ax_3d.set_zlabel('z')
    ax_xy.set_title('XY Projection')
    ax_xy.set_xlabel('x')
    ax_xy.set_ylabel('y')
    ax_xz.set_title('XZ Projection')
    ax_xz.set_xlabel('x')
    ax_xz.set_ylabel('z')
    ax_yz.set_title('YZ Projection')
    ax_yz.set_xlabel('y')
    ax_yz.set_ylabel('z')
    
    ax_3d.set_xlim(0,1440)
    ax_3d.set_ylim(0,1440)
    ax_3d.set_zlim(1700,1724)
    ax_xy.set_xlim(0,1440)
    ax_xy.set_ylim(0,1440)
    ax_xz.set_xlim(0,1440)
    ax_xz.set_ylim(1700,1724)
    ax_yz.set_xlim(0,1440)
    ax_yz.set_ylim(1700,1724)
    
    return (ax_3d, ax_xy, ax_xz, ax_yz)

def get_well_coords(well):
    xs = [well.heel[0], well.toe[0]]
    ys = [well.heel[1], well.toe[1]]
    zs = [well.heel[2], well.toe[2]]
    return (xs, ys, zs)

def plot_3d_well(ax, well, style):
    (xs, ys, zs) = get_well_coords(well)
    ax.plot(xs, ys, zs, style)
    
def plot_2d_well_xy(ax, well, style):
    (xs, ys, zs) = get_well_coords(well)
    ax.plot(xs, ys, style)
    
def plot_2d_well_xz(ax, well, style):
    (xs, ys, zs) = get_well_coords(well)
    ax.plot(xs, zs, style)
    
def plot_2d_well_yz(ax, well, style):
    (xs, ys, zs) = get_well_coords(well)
    ax.plot(ys, zs, style)

3.2 Base Case

3.2.1 Well Data


In [8]:
for well in [well_base_injector, well_base_producer]:
    print(well.name)
    print('\tHeel: ', well.heel)
    print('\tToe: ', well.toe)


INJECTOR
	Heel:  [300.0, 500.0, 1712.0]
	Toe:  [772.0, 500.0, 1712.0]
PRODUCER
	Heel:  [300.0, 900.0, 1712.0]
	Toe:  [900.0, 900.0, 1712.0]

3.2.2 Well Path Plots


In [9]:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_base = plt.figure()
(ax_base_wells_3d, ax_base_wells_xy, ax_base_wells_xz, ax_base_wells_yz) = setup_wp_figure(fig_well_paths_base)

plot_3d_well(ax_base_wells_3d, well_base_injector, 'b')
plot_3d_well(ax_base_wells_3d, well_base_producer, 'r--')

plot_2d_well_xy(ax_base_wells_xy, well_base_injector, 'b')
plot_2d_well_xy(ax_base_wells_xy, well_base_producer, 'r--')

plot_2d_well_xz(ax_base_wells_xz, well_base_injector, 'b')
plot_2d_well_xz(ax_base_wells_xz, well_base_producer, 'r--')

plot_2d_well_yz(ax_base_wells_yz, well_base_injector, 'b')
plot_2d_well_yz(ax_base_wells_yz, well_base_producer, 'r--')


3.3 Tentative Best Case

3.3.1 Well Data


In [10]:
for well in [well_tentative_best_injector, well_tentative_best_producer]:
    print(well.name)
    print('\tHeel: ', well.heel)
    print('\tToe: ', well.toe)


INJECTOR
	Heel:  [108.0, 500.0, 1712.0]
	Toe:  [644.0, 500.0, 1712.0]
PRODUCER
	Heel:  [44.0, 1412.0, 1712.0]
	Toe:  [324.0, 708.0, 1712.0]

3.3.2 Well Path Plots


In [11]:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_best = plt.figure()
(ax_best_wells_3d, ax_best_wells_xy, ax_best_wells_xz, ax_best_wells_yz) = setup_wp_figure(fig_well_paths_best)

plot_3d_well(ax_best_wells_3d, well_tentative_best_injector, 'b')
plot_3d_well(ax_best_wells_3d, well_tentative_best_producer, 'r--')

plot_2d_well_xy(ax_best_wells_xy, well_tentative_best_injector, 'b')
plot_2d_well_xy(ax_best_wells_xy, well_tentative_best_producer, 'r--')

plot_2d_well_xz(ax_best_wells_xz, well_tentative_best_injector, 'b')
plot_2d_well_xz(ax_best_wells_xz, well_tentative_best_producer, 'r--')

plot_2d_well_yz(ax_best_wells_yz, well_tentative_best_injector, 'b')
plot_2d_well_yz(ax_best_wells_yz, well_tentative_best_producer, 'r--')


3.4 Latest Case

3.4.1 Well Data


In [12]:
for well in [well_latest_injector, well_latest_producer]:
    print(well.name)
    print('\tHeel: ', well.heel)
    print('\tToe: ', well.toe)


INJECTOR
	Heel:  [108.0, 500.0, 1712.0]
	Toe:  [644.0, 500.0, 1712.0]
PRODUCER
	Heel:  [44.0, 1412.0, 1712.0]
	Toe:  [324.0, 740.0, 1712.0]

3.4.2 Well Path Plots


In [13]:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
fig_well_paths_latest = plt.figure()
(ax_latest_wells_3d, ax_latest_wells_xy, ax_latest_wells_xz, ax_latest_wells_yz) = setup_wp_figure(fig_well_paths_latest)

plot_3d_well(ax_latest_wells_3d, well_latest_injector, 'b')
plot_3d_well(ax_latest_wells_3d, well_latest_producer, 'r--')

plot_2d_well_xy(ax_latest_wells_xy, well_latest_injector, 'b')
plot_2d_well_xy(ax_latest_wells_xy, well_latest_producer, 'r--')

plot_2d_well_xz(ax_latest_wells_xz, well_latest_injector, 'b')
plot_2d_well_xz(ax_latest_wells_xz, well_latest_producer, 'r--')

plot_2d_well_yz(ax_latest_wells_yz, well_latest_injector, 'b')
plot_2d_well_yz(ax_latest_wells_yz, well_latest_producer, 'r--')


4 Simulation Statistics

4.1 Simulator Execution Times


In [14]:
from matplotlib import pyplot as plt
fig_sim_exec_time = plt.figure()
fig_sim_exec_time.set_size_inches(10,6)
ax_sim_exec_time = fig_sim_exec_time.add_subplot(121)
ax_sim_exec_time.plot([i+1 for i in range(len(simulator.durations))],
                      simulator.durations)
ax_sim_exec_time.set_xlabel('Simulation #')
ax_sim_exec_time.set_ylabel('Execution time [seconds]')

ax_sim_exec_time = fig_sim_exec_time.add_subplot(122)
ax_sim_exec_time.semilogy([i+1 for i in range(len(simulator.durations))],
                      simulator.durations)
ax_sim_exec_time.set_xlabel('Simulation #')
ax_sim_exec_time.set_ylabel('log(Execution time [seconds])')


Out[14]:
<matplotlib.text.Text at 0x7f0a3e64ff98>

In [15]:
import numpy as np
simulator_durations = np.array(simulator.durations)
print('Min:\t', simulator_durations.min(), 's')
print('Max:\t', simulator_durations.max(), 's')
print('Median:\t', np.median(simulator_durations), 's')
print('Mean:\t', simulator_durations.mean(), 's')


Min:	 1 s
Max:	 750 s
Median:	 4.0 s
Mean:	 17.103125 s

4.3 Result History


In [18]:
from matplotlib import pyplot as plt

fig_result_history = plt.figure()
fig_result_history.set_size_inches(10,6)
ax_result_history = fig_result_history.add_subplot(111)
for case_id in production_data.time:
    ax_result_history.plot(production_data.time[case_id], production_data.fopt[case_id], 'r')
    ax_result_history.plot(production_data.time[case_id], production_data.fwpt[case_id], 'b')
    ax_result_history.plot(production_data.time[case_id], production_data.fgpt[case_id], 'g')


5 Well Placement Animation


In [19]:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

# Attaching 3D axis to the figure and setting props
fig_anim = plt.figure()
fig_anim.set_size_inches(14,12)
ax_anim = p3.Axes3D(fig_anim)
ax_anim.set_xlim(0,1440)
ax_anim.set_ylim(0,1440)
ax_anim.set_zlim(1700,1724)
ax_anim.set_xlabel('x')
ax_anim.set_ylabel('y')
ax_anim.set_zlabel('z')

def anim_get_well_lines(number):
    wp = optimizer.best_case_pr_iteration[number].get_well("PRODUCER")
    wi = optimizer.best_case_pr_iteration[number].get_well("INJECTOR")
    return [[wp.heel, wp.toe], [wi.heel, wi.toe]]



def anim_update_well_lines(num, well_data, lines):
    for i in range(len(lines)):
        lines[i].set_data(well_data[num,i,:,0:2])
        lines[i].set_3d_properties(well_data[num,i,:,2])
    return lines

# The data to be plotted in a 4D array [case, inj/prod, heel/toe, x/y/z]
anim_well_data = np.array([anim_get_well_lines(cn) for cn in range(len(optimizer.best_case_pr_iteration))])


# Initializing lines. One line per well for the first best case.
anim_well_lines = [ax_anim.plot(well[:,0], well[:,1], well[:,2])[0] for well in anim_well_data[0]]
anim_well_lines[0].set_color('r')
anim_well_lines[1].set_color('b')
anim_well_lines[0].set_linestyle('--')
anim_well_lines[0].set_linewidth(4)
anim_well_lines[1].set_linewidth(4)


# FuncAnimation(figure, function, frames, fargs=(), interval,blit)
# figure: The figure in which to draw the animation
# function: function that will be called for each frame. The frame number will be passed as the first argument,
#           followed by the arguments specified in fargs.
# frames: number of frames to draw; the number will be passed as first arg to the function ([0:frames])
# fargs will be passed as arguments to the function
# interval: ms between frames
line_ani = animation.FuncAnimation(fig_anim, anim_update_well_lines, len(optimizer.best_case_pr_iteration),
                                   fargs=(anim_well_data, anim_well_lines),
                                   interval=1000)

# Define a video tag for the browser
from tempfile import NamedTemporaryFile
VIDEO_TAG = '''<video controls autoplay>
 <source src="data:{0}">
 Your browser does not support the video tag.
</video>'''

# Function to convert the animation to a video format supported by the browser.
# Requires ffmpeg to be installed on the system.
def anim_to_html(anim):
    if not hasattr(anim, '_encoded_video'):
        with NamedTemporaryFile(suffix='.mp4') as f:
            anim.save(f.name, fps=1, extra_args=['-vcodec', 'libx264'])
            video = open(f.name, "rb").read()
        anim.to_html5_video()
    return VIDEO_TAG.format(anim.to_html5_video())

# Function that calls the converter function and returns HTML code to be displayed in the browser.
from IPython.display import HTML
def display_animation(anim):
    plt.close(anim._fig)
    return HTML(anim_to_html(anim))

# Display the video in the browser.
display_animation(line_ani)


Out[19]:
"> Your browser does not support the video tag.

In [85]: